/*
 * Copyright (c) 2014. kupat Corporation. All rights reserved.
 * see statement on http://www.kupat.cn.
 */

package com.kfgj.hkp.common.components.io;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liwensihan on 2015-7-31.
 *
 * @author liwensihan
 */
public class ByteBinaryHelper {

    public static byte[] binaryTobytes(String binary){
        int i = binary.length()%8;
        if (i != 0) {
            String tmp = "";
            for (int j = 0; j < 8-i; j++) {
                tmp +="0";
            }
            binary = tmp+binary;
        }
        List<String> ls = new ArrayList<String>();
        for (int j = 0; j <= binary.length()-8;) {
            ls.add(binary.substring(j, j+8));
            j +=8;
        }
        byte[] bt = new byte[ls.size()];
        for (int j = 0; j < bt.length; j++) {
            bt[j] = binaryToByte(ls.get(j));
        }
        return bt;
    }
    /**
     * 如果是
     * @param binary
     * @return
     */
    public static byte binaryToByte(String binary){
        if (binary.length()>8) {
            binary = binary.substring(binary.length()-8,binary.length());
        }else if (binary.length()<8) {
            for (int i = 0; i < 8-binary.length(); i++) {
                binary = "0"+binary;
            }
        }
        int[] it = new int[8];
        for (int i = 0; i < it.length; i++) {
            it[i] = Integer.valueOf(binary.substring(i,i+1));
        }
        boolean negative = false;
        if (it[0] == 1) {
            negative = true;
            for (int i = 0; i < it.length; i++) {
                if (it[i]==1) {
                    it[i]=0;
                }else if(it[i]==0){
                    it[i]=1;
                }
            }
            int add=1;
            for (int i = it.length-1; i >=0; i--) {
                if (add==1 && it[i]==1) {
                    it[i]=0;
                }else if(add==1 && it[i]==0){
                    it[i]=1;
                    add=0;
                }else{
                    break;
                }
            }
        }
        int result=0;
        int k =7;
        for (int i = 0; i < it.length; i++) {
            if (it[i]==1) {
                result += power(2, k);
            }
            k--;
        }
        return (byte) (negative==true?-result:result);
    }
    /**
     * 返回x的y次方
     * @param x
     * @param y
     * @return
     */
    public static int power(int x,int y){
        int result = x;
        if (y==0) {
            return 1;
        }else if (y==1) {
            return x;
        }
        for (int i = 0; i < y-1; i++) {
            result *=x;
        }
        return result;
    }
    public static String bytesToBinary(byte[] bs) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bs) {
            sb.append(byteToBinary(b));
        }
        return sb.toString();
    }
    public static String byteToBinary(byte bt){
        String tmp = Integer.toBinaryString(bt);
        int len = tmp.length();
        if (tmp.length()>8) {
            tmp = tmp.substring(len-8,len);
        }else if (tmp.length()<8) {
            for (int i = 0; i < 8-len; i++) {
                tmp = "0"+tmp;
            }
        }
        return tmp;

    }
}
